package com.zx.framework.core.util.layer;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zx.framework.core.util.StringUtil;

public class IDUtil {
	
	/**
	 * layer的长度以3为分割
	 */
	public static final int LENGTH = 3;
	/**
	 * 前缀的分隔符 zxkj-001
	 */
	public static final String SPLIT = "-";
	/**
	 *每个layer的最大值
	 */
	public static final String MAX = "zzz";
	/**
	 *每个layer的最小值
	 */
	public static final String MIN = "000";
	
	public static final int RADIX = 36;
	/**
	 * 最大10进制值
	 */
	private static long s = Long.parseLong(MAX, RADIX);
	
	/**
	 * 将传入的36进制数据+1
	 * @param input36 输入值
	 * @param parent 父类值
	 * @return
	 */
	public static final String increase(String input36,String parent){
		if(StringUtil.isEmpty(parent)){
			return MIN;
		}
		if(StringUtil.isEmpty(input36)){
			return parent+MIN;
		}
		if(input36.length()%3!=0){
			throw new RuntimeException("length is not available");
		}
		Pattern pattern = Pattern.compile("([\\w]{3})");
		Matcher matcher = pattern.matcher(input36);
		List<String> group = new ArrayList<String>();
		while (matcher.find()) {
			group.add(matcher.group(0));
		}
		String last = group.get(group.size()-1);
		long temp = Long.parseLong(last, RADIX);
		if(temp+1>s){
			throw new RuntimeException("Out of range[36 radix]");
		}
		String val = Long.toString(temp+1, RADIX);
		val = StringUtil.repeat("0", MAX.length()-val.length())+val;
		group.set(group.size()-1, val);
		StringBuilder sb = new StringBuilder();
		for (String string : group) {
			sb.append(string);
		}
		return sb.toString();
	}
	public static void main(String[] args) {
		String s1 = "001001zzz";
		System.out.println(increase(s1, "001001"));
	}
}
